<template>
  <div class="app-container">

    <u-page title="企划每日报表">

      <u-filtered>
        <el-form :inline="true" :model="listQuery" class="search-form">
          <el-form-item label="日期选择" prop="inputDateRange">
            <el-date-picker
              v-model="inputDateRange"
              value-format="yyyy-MM-dd"
              type="daterange"
              align="right"
              unlink-panels
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              :picker-options="pickerOptions"
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" @click="handleFilter">搜索</el-button>
            <el-button type="primary" icon="el-icon-download" @click="handleDownload">导出</el-button>
          </el-form-item>
        </el-form>
      </u-filtered>

      <u-table
        :data="list"
        :loading="listLoading"
        :options="listOptions"
        :columns="columns"
        :pagination.sync="listQuery"
        :fetch="hdlList"
        @selection-change="hdlSelectChange"
      />

    </u-page>
  </div>
</template>
<script>
export default {
  data() {
    return {
      api: this.$u.api.DataPlanningdailydata,
      // 表格
      columns: [
        {
          prop: 'statdatestr',
          label: '日期',
          width: '120'
        },
        {
          prop: 'custnos',
          label: '客流',
          width: '90'
        },
        {
          prop: 'cars',
          label: '车流',
          width: '90'
        },
        {
          prop: 'budget',
          label: '全馆预算',
          width: '100'
        },
        {
          prop: 'turnover',
          label: '全馆业绩',
          width: '100'
        },
        {
          prop: 'dc',
          label: '达成率',
          width: '100'
        },
        {
          prop: 'salesvolume',
          label: '全馆交易笔数',
          width: '100'
        },
        {
          prop: 'tdl',
          label: '提袋率',
          width: '100'
        },
        {
          prop: 'vipturnover',
          label: '会员业绩',
          width: '100'
        },
        {
          prop: 'vipzb',
          label: '会员业绩占比',
          width: '100'
        },
        {
          prop: 'vipsalesvolume',
          label: '会员交易笔数',
          width: '100'
        },
        {
          prop: 'vipbdj',
          label: '会员交易笔单价',
          width: '120'
        },
        {
          prop: 'vipcustomers',
          label: '会员交易客数',
          width: '100'
        },
        {
          prop: 'vipkd',
          label: '会员交易客单',
          width: '100'
        },
        {
          prop: 'vipcustomers_cy',
          label: '会员交易客数_餐饮',
          width: '130'
        },
        {
          prop: 'vipcustomers_bh',
          label: '会员交易客数_百货',
          width: '130'
        },
        {
          prop: 'newvipnumbers',
          label: '新办卡会员数',
          width: '100'
        }
      ],
      list: [],
      inputDateRange: [],
      pickerOptions: {
        shortcuts: [{
          text: '最近一周',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近一个月',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近三个月',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
            picker.$emit('pick', [start, end])
          }
        }]
      },
      listLoading: false,
      listOptions: {
        mutiSelect: true,
        stripe: true
      },
      listQuery: {
        page: 1,
        pageSize: 50,
        totalCount: 1
      }
    }
  },
  created() {
  },
  methods: {
    // 记账日期格式转换
    inputdateFormat() {
      if (this.inputDateRange) {
        this.listQuery.beginTime = this.inputDateRange[0]
        this.listQuery.endTime = this.inputDateRange[1]
      } else {
        this.listQuery.beginTime = ''
        this.listQuery.endTime = ''
      }
    },
    // 搜索事件
    handleFilter() {
      this.inputdateFormat()
      this.hdlFilter()
    },
    /**
     * 查询事件
     */
    hdlFilter() {
      this.listQuery.page = 1
      this.hdlList()
    },
    /**
     * 获取列表数据
     */
    hdlList() {
      const self = this
      // 打开加载状态
      self.listLoading = true
      self.api.list(self.listQuery).then(res => {
        // 获取分页列表数据
        self.list = res.data.list
        // fix:修复ID重复的问题,设置table表格key值必须唯一
        self.list.forEach((item, index) => {
          self.list[index].index = index
        })
        // 获取分页数据,总页数
        self.listQuery.totalCount = res.data.totalCount
        // 关闭加载状态
        setTimeout(() => {
          self.listLoading = false
        }, 0.5 * 1000)
      })
    },
    // 导出数据
    handleDownload() {
      // 复制值
      const list = this.$u.deepClone(this.list)
      // 业务数据处理
      console.log(list)
      // 表头&表数据
      const tHeader = ['日期', '客流', '车流', '全馆预算', '全馆业绩', '达成率', '全馆交易笔数', '提袋率', '会员业绩',
        '会员业绩占比', '会员交易笔数', '会员交易笔单价', '会员交易客数', '会员交易客单', '会员交易客数_餐饮', '会员交易客数_百货', '新办卡会员数']
      const filterVal = ['statdatestr', 'custnos', 'cars', 'budget', 'turnover', 'dc', 'salesvolume', 'tdl', 'vipturnover',
        'vipzb', 'vipsalesvolume', 'vipbdj', 'vipcustomers', 'vipkd', 'vipcustomers_cy', 'vipcustomers_bh', 'newvipnumbers']
      const data = list.map(v => filterVal.map(j => { return v[j] }))
      this.$u.exports.excel('企划日报', tHeader, data)
    }
  }
}
</script>
<style>
.el-select .el-input {
  width: 220px;
}
</style>
<style lang="scss" scoped>
.app-container{
  .item >>> .el-form-item__content {
    display: flex;
  }
  .status {
    ::v-deep{
      .el-input{
        width: 140px;
      }
    }
  }
  ::v-deep{
    .el-table-expand {
      font-size: 0;

      label {
        width: 90px;
        color: #99a9bf;
        font-size: 12px;
      }

      .el-form-item{
        margin-bottom: 2px;
        margin-right: 0;
        vertical-align: top;
      }

      .el-form-item__content {
        margin-right: 0;
        margin-bottom: 0;
        font-size: 12px;
        display: inline-block;
        vertical-align: top;
        position: relative;
      }
    }
  }
}
</style>
